package com.hcbd.tdwt.base.other;

import android.app.Service;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;


import com.hcbd.tdwt.MainApp;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.Constant;

import java.io.IOException;

/**
 * @author Is-Poson
 * @time 2017/9/13  11:05
 * @desc 提示音 + 手机震动管理类
 */

public class BeeAndVibrateManager {

    private static boolean shouldPlayBeep = true;

    /**
     * 震动
     */
    public static void vibrate() {
        Vibrator vibrator = (Vibrator) MainApp.getInstance().getSystemService(Context.VIBRATOR_SERVICE);
        long[] pattern = {100, 300};   // 停止 开启 停止 开启
        vibrator.vibrate(pattern, -1);// 如果只想震动一次，index设为-1
    }

    /**
     * @param milliseconds 震动时长 , 单位毫秒
     */
    public static void vibrate(long milliseconds) {
        Vibrator vibrator = (Vibrator) MainApp.getInstance().getSystemService(Context.VIBRATOR_SERVICE);
        long[] pattern = {100, milliseconds};   // 停止 开启 停止 开启
        vibrator.vibrate(pattern, -1);// 如果只想震动一次，index设为-1
    }

    /**
     * @param pattern  自定义震动模式 。数组中数字的含义依次是[静止时长，震动时长，静止时长，震动时长。。。]单位是毫秒
     * @param isRepeat true-> 反复震动，false-> 只震动一次
     */
    public static void vibrate(long[] pattern, boolean isRepeat) {
        Vibrator vib = (Vibrator) MainApp.getInstance().getSystemService(Service.VIBRATOR_SERVICE);
        vib.vibrate(pattern, isRepeat ? 1 : -1);
    }

    public static void playBee(final PlayerCompleteListener listener) {
        AudioManager audioService = (AudioManager) MainApp.getInstance()
                .getSystemService(Context.AUDIO_SERVICE);
        if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
            shouldPlayBeep = false;//检查当前是否是静音模式
        }

        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer player) {
                player.seekTo(0);
            }
        });
        try {
        AssetFileDescriptor file = MainApp.getInstance().getResources().openRawResourceFd(R.raw.dingdon);

            mediaPlayer.setDataSource(file.getFileDescriptor(),
                    file.getStartOffset(), file.getLength());
            file.close();
            mediaPlayer.setVolume(0, 1);
            mediaPlayer.prepare();
        } catch (IOException ioe) {
            mediaPlayer = null;
        }

        if (shouldPlayBeep && mediaPlayer != null) {
            mediaPlayer.start();
        }
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.stop();
                listener.onCompletion(mp);
            }
        });
    }

    /**
     * 震动和响铃
     * @param listener
     */
    public static void playBeeAndVibrate(PlayerCompleteListener listener) {
        if (Constant.Y.equals(Constant.bg_not_isOpen)){return;}  // 消息免打扰
        if (Constant.Y.equals(Constant.shock_isOpen)) {
            //震动
            vibrate();
        }
        if (Constant.Y.equals(Constant.sound_isOpen)){
            //提示音
            playBee(listener);
        }
    }

    //MediaPlayer播放完毕监听
    public interface PlayerCompleteListener {
        void onCompletion(MediaPlayer mp);
    }
}
